package chd.edu.spzx.product.service.impl;

import chd.edu.spzx.model.entity.product.Category;
import chd.edu.spzx.product.mapper.CategoryMapper;
import chd.edu.spzx.product.service.CategoryService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author Zhang Chaoliang
 * create 2023
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 所有一级分类
     * @return
     */
    @Override
    public List<Category> selectOneCategory() {

        //1. 查询redis，是否包含所有的一级分类
        String categoryOneJson = redisTemplate.opsForValue().get("category:one");

        //2. 如果redis包含所有一级分类，直接返回
        if (StringUtils.hasText(categoryOneJson)){
            //categoryOneJson 字符串转换List<Category>
            List<Category> existcategoryList = JSON.parseArray(categoryOneJson, Category.class);
            return existcategoryList;
        }

        //3. 如果redis没有所有一级分类，查询数据库，把数据库查询内容返回，并且查询内容放到redis里面
        List<Category> categoryList = categoryMapper.selectOneCategory();
        //查询内容放到redis里面
        redisTemplate.opsForValue().set("category:one",
                                        JSON.toJSONString(categoryList),
                                7, TimeUnit.DAYS);

        return categoryList;
    }

    /**
     * 查询所有的分类，树形封装
     * @return
     */
    @Cacheable(value = "category",key = "'all'")   //category::all
    @Override
    public List<Category> findCategoryTree() {

        //1. 查询所有分类，返回list集合
        List<Category> categoryList = categoryMapper.findAll();

        //2. 遍历所有分类list集合，通过条件 parentid=0得到以及下面二级分类
        List<Category> oneCategoryList =
                categoryList.stream()
                        .filter(item -> item.getParentId().longValue() == 0)
                        .collect(Collectors.toList());

        //3. 遍历所有一级分类list集合，条件判断：id = parentid,得到一级下面的二级分类
        oneCategoryList.forEach(oneCategory -> {
            List<Category> twoCategoryList =
                    categoryList.stream()
                            .filter(item -> item.getParentId().longValue() == oneCategory.getId().longValue())
                            .collect(Collectors.toList());

            //把二级分类封装到一级分类中去
            oneCategory.setChildren(twoCategoryList);

            //4. 遍历所有的二级分类，条件判断： id = parentid ,得到二级下面的三级分类
            twoCategoryList.forEach(twoCategory -> {
                List<Category> threeCategoryList =
                        categoryList.stream()
                                .filter(item -> item.getParentId().longValue() == twoCategory.getId().longValue())
                                .collect(Collectors.toList());

                //把三级分类封装到二级分类中去
                twoCategory.setChildren(threeCategoryList);
            });
        });


        return oneCategoryList;
    }
}
